package cn.edu.lzzy.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Component
public class MVCConfig implements WebMvcConfigurer {

        @Value("${user.picPath}")
        private String imgPath;
        @Value("${pic.size}")
        private int maxPicSize;

        @Override
        public void addViewControllers(ViewControllerRegistry registry){
        //这样就不需要为每一个页面编写Controller代码了，对于纯渲染的页面可以直接这样定义就可以
        registry.addViewController("/home").setViewName("index");//forWard实现
        }
//        上传文件大小限制
        @Bean
        public CommonsMultipartResolver multipartResolver(){
                CommonsMultipartResolver resolver=new CommonsMultipartResolver();
                resolver.setMaxUploadSize(maxPicSize);
                return resolver;
        }

        /***
         * 文件上传路径
         * @param registry
         */

        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
                //.addResourceHandler前面部分默认静态资源目录
                registry.addResourceHandler("/**")
                        .addResourceLocations(//默认静态资源目录
                                "classpath://META-INF/resources",
                                "classpath:/resources/",
                                "classpath:/static/",
                                "classpath:/public/",
                                "classpath:/webapp/",
                                imgPath
                        );
        }
}
